//
//  WHomeViewController.swift
//  iiatn
//
//  Created by mnine on 18/3/28.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SVProgressHUD


let comdproCellID = "comdproCellID"
class WHomeViewController: itBaseViewController, HomeTwoViewDelegate, UITableViewDelegate, UITableViewDataSource{
    
    var fuwuVC: UIViewController?
    var zixunVC: UIViewController?
    
    var items = [WPro]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(scrollView)
        setmoreview()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /// 懒加载创建 scrollView
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.isScrollEnabled = true
        scrollView.backgroundColor = WGlobalWhiteColor()
        scrollView.frame = CGRect(x: 0, y: 70, width: SCREENW, height: SCREENH)
        return scrollView
    }()
    
    func setmoreview(){
        setlunbo()
        setfbxz86()
        setcommond()
    }
    
    func setlunbo(){
        WNetwork.shareNetworkTool.loadLunBo(){ (hotnews5) in
            let frame = CGRect(x: 0, y: 36, width: SCREENW, height: SCREENW/2)
            let imagesURLStrings = [
                hotnews5[0].thumb!,
                hotnews5[1].thumb!,
                hotnews5[2].thumb!,
                hotnews5[3].thumb!,
                hotnews5[4].thumb!
                ]
            var titles = [String]()
            for hotnew in hotnews5 {
                if (hotnew.title?.characters.count)! > 20 {
                    let startIndex = hotnew.title?.index((hotnew.title?.startIndex)!, offsetBy:0)// 从第几个开始
                    let endIndex = hotnew.title?.index(startIndex!, offsetBy:20)//截取几个
                    hotnew.title! = (hotnew.title?.substring(with: startIndex!..<endIndex!))! + "......"
                }
                hotnew.title! = "   " + hotnew.title!
                titles.append(hotnew.title!)
            }

            let cycleScrollView = LLCycleScrollView.cycleScrollView(frame: frame, imageURLGroup: imagesURLStrings as NSArray)
            cycleScrollView.pageControlAliment = LLCycleScrollViewPageContolAliment.LLCycleScrollViewPageContolAlimentRight
            cycleScrollView.titlesGroup = titles as NSArray
            cycleScrollView.callBackWithIndex = { (index : Int) in
                print("当前网络图片 Index:\(index)")
                self.toZixunDetail(item: hotnews5[index])
            }
            self.scrollView.addSubview(cycleScrollView)
        }
    }
    
    func setfbxz86(){
        // 添加顶部view 并给topview添加控制器试图controller.view
        let TwoView = UIView(frame: CGRect(x: 0, y: SCREENW/2+46, width: SCREENW, height: 670))
        let WHomeTwoViewController = WHomeTwoView()
        WHomeTwoViewController.delegate = self
        addChildViewController(WHomeTwoViewController)
        let twoVC = childViewControllers[0]
        TwoView.addSubview(twoVC.view)
        scrollView.addSubview(TwoView)
        
        scrollView.contentSize = CGSize(width: SCREENW, height: TwoView.frame.maxY+100)
    }

    func setcommond(){
        WNetwork.shareNetworkTool.loadCommond() { (commond5) in
            self.items = commond5
            let tv = UITableView(frame: CGRect(x: 0, y: self.scrollView.height + 180, width: SCREENW, height: 750))
            tv.rowHeight = 150
            tv.dataSource = self
            tv.delegate = self
            let nib = UINib(nibName: String(describing: WProViewCell.self), bundle: nil)
            tv.register(nib, forCellReuseIdentifier: comdproCellID)
            
            self.scrollView.addSubview(tv)
            self.scrollView.contentSize = CGSize(width: SCREENW, height: tv.frame.maxY+150)
        }
    }
    
    
    func toZixunDetail(item: WZiXun){
        let detailVC = WZixunDetailViewController()
        detailVC.zixunItem = item
        detailVC.title = " "
        navigationController?.pushViewController(detailVC, animated: true)
    }

    
    // MARK: - HomeTwoViewDelegate
    func homeTwo(button: UIButton) {
        print(button.tag)
        switch button.tag{
        case 1:
            toXZ()
        case 2:
            toFB()
        case 3:
            toPro(id: 3)
        case 4:
            toPro(id: 4)
        case 5:
            toPro(id: 5)
        case 6:
            toPro(id: 6)
        case 7:
            toZixun(id: 7)
        case 8:
            toZixun(id: 8)
        case 9:
            SVProgressHUD.showInfo(withStatus: "该栏目暂无内容！")
        case 10:
            toPro(id: 10)
        case 11:
            toPro(id: 11)
        case 12:
            toPro(id: 12)
        case 13:
            toPro(id: 13)
        case 14:
            toPro(id: 14)
        case 15:
            toPro(id: 15)
        case 16:
            toPro(id: 16)
        default:
            print("no button.tag")
        }
    }
    
    func toXZ(){
        let alertController = UIAlertController(title: "寻找项目",
                                                message: "\n请输入项目名称或关键字", preferredStyle: .alert)
        alertController.addTextField {
            (textField: UITextField!) -> Void in
            textField.placeholder = "请输入项目名称或关键字"
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "确认", style: .default, handler: {
            action in
            //也可以用下标的形式获取textField let login = alertController.textFields![0]
            let key = alertController.textFields!.first!
            print("关键字：\(key.text!)")
            if key.text!.isEmpty {
                SVProgressHUD.showError(withStatus: "您什么都没有输入！")
            } else {
                self.toSearch(key: key.text!)
            }
            
            
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
    func toFB(){
        if whaslogin!{
            let detailVC = WFBProViewController()
            detailVC.title = "发布项目"
            navigationController?.pushViewController(detailVC, animated: true)
        }else{
            let alertController = UIAlertController(title: "会员专享",
                                                    message: "您还没有登录，是否前往登录？", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let okAction = UIAlertAction(title: "确认", style: .default, handler: {
                action in
                self.toLogin()
                
            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
    func toSearch(key: String){
        let detailVC = WSearchProViewController()
        detailVC.title = " "
        detailVC.key = key
        navigationController?.pushViewController(detailVC, animated: true)
    }
    func toLogin(){
        let loginVC = WLoginViewController()
        loginVC.title = "登录"
        let nav = WNavigationController(rootViewController: loginVC)
        present(nav, animated: true, completion: nil)
    }
    
    func toPro(id: Int){
        switch id {
        case 3:
            fuwuVC = WPro1ViewController()
            fuwuVC?.title = "海外投资"
        case 4:
            fuwuVC = WPro2ViewController()
            fuwuVC?.title = "基础设施"
        case 5:
            fuwuVC = WPro4ViewController()
            fuwuVC?.title = "境外园区"
        case 6:
            fuwuVC = WPro5ViewController()
            fuwuVC?.title = "产能合作"
        case 10:
            fuwuVC = WProhViewController()
            fuwuVC?.title = "最热项目"
        case 11:
            fuwuVC = WPro40ViewController()
            fuwuVC?.title = "咨询服务"
        case 12:
            fuwuVC = WPro6ViewController()
            fuwuVC?.title = "海外安保"
        case 13:
            fuwuVC = WPro7ViewController()
            fuwuVC?.title = "人才培训"
        case 14:
            fuwuVC = WPro37ViewController()
            fuwuVC?.title = "法律服务"
        case 15:
            fuwuVC = WPro38ViewController()
            fuwuVC?.title = "审计服务"
        case 16:
            fuwuVC = WPro44ViewController()
            fuwuVC?.title = "翻译服务"
        default:
            print("Pro no this id ")
        }
        navigationController?.pushViewController(fuwuVC!, animated: true)
    }
    
    func toZixun(id: Int){
        switch id {
        case 7:
            zixunVC = WZixunZHViewController()
            zixunVC?.title = "展会公告"
        case 8:
            zixunVC = WZixunZYViewController()
            zixunVC?.title = "投资指引"
        default:
            print("Pro no this id ")
        }
        navigationController?.pushViewController(zixunVC!, animated: true)
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    // 一共多少行
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }
    
    // 行数据定义
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: comdproCellID) as! WProViewCell
        cell.selectionStyle = .gray
        cell.proItem = items[indexPath.row]
        
        return cell
    }
    
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if whaslogin!{
            let detailVC = WProDetailViewController()
            detailVC.proItem = items[indexPath.row]
            detailVC.title = " "
            navigationController?.pushViewController(detailVC, animated: true)
        }else{
            let alertController = UIAlertController(title: "会员专享",
                                                    message: "您还没有登录，是否前往登录？", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let okAction = UIAlertAction(title: "确认", style: .default, handler: {
                action in
                self.toLogin()
                
            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}
